BEGINTRANS Statement ---------------------------------------------------------------------------- Action Indicates the beginning of a transaction (a series of ISAM database operations). Syntax BEGINTRANS Remarks Transactions are a way to group a series of ISAM operations so that you can commit them as a whole, rescind them all, or rescind operations since a designated savepoint. Use the COMMITTRANS statement to commit a transaction. Use the SAVEPOINT function to designate a savepoint. Use the ROLLBACK and ROLLBACK ALL statements to rescind all or part of a transaction's operations. If you attempt to use BEGINTRANS when there already is a transaction pending, BASIC generates the error message Illegal function call. Any ISAM operation that closes a table causes transactions to be committed. For example, if a type mismatch occurs while you are opening an ISAM table, the table is closed and a pending transaction is committed. You may wish to code your programs so they open all tables, then perform all transactions, then close tables. Make sure any operation that can close a table occurs outside a transaction. See Also COMMITTRANS, ROLLBACK, SAVEPOINT Example The following example uses the DELETE statement to remove records from an ISAM file. It creates a transaction with the BEGINTRANS and COMMITTRANS statements, and uses SAVEPOINT and ROLLBACK to provide rollback of any or all of the deletions. The program uses the file called BOOKS.MDB, which SETUP copies to your disk. DEFINT A-Z TYPE Borrower Cardnum AS LONG ' Card number. TheName AS STRING * 36 ' Name. Address AS STRING * 50 ' Address. City AS STRING * 26 ' City. State AS STRING * 2 ' State. Zip AS LONG ' Zip code. END TYPE DIM People AS Borrower ' Record structure variable. CONST Database = "BOOKS.MDB" ' Name of the disk file. CONST Tablename = "Cardholders" ' Name of the table. CONST viewbottom = 17, viewtop = 3, msgtxt = " *** Deleted. Savepoint " DIM SavePts(viewbottom - viewtop + 1) TableNum = FREEFILE OPEN Database FOR ISAM Borrower Tablename AS TableNum ' Loop until user chooses to quit. DO VIEW PRINT CLS . COLOR 15, 0 PRINT SPC(34); "Card Holders" PRINT "Card#"; SPC(2); "Name"; SPC(13); "Address"; PRINT SPC(20); "City"; SPC(6); "State"; SPC(2); "Zip" LOCATE 24, 1. PRINT "Choose a key. "; PRINT "D - Delete this record N - Next record Q - Quit"; MOVEFIRST TableNum VIEW PRINT viewtop TO viewbottom. COLOR 7, 0. CLS vPos = viewtop ' Loop through a screenful of records. DO ' For each record, display and ask user what to do with it. RETRIEVE TableNum, People LOCATE vPos, 1 PRINT USING ("#####"); People.Cardnum; PRINT " "; LEFT$(People.TheName, 15); " "; PRINT LEFT$(People.Address, 25); " "; PRINT LEFT$(People.City, 10); " "; People.State; " "; PRINT USING ("#####"); People.Zip ' Get keystroke from user. validkeys$ = "DNQ" DO keychoice$ = UCASE$(INKEY$) LOOP WHILE INSTR(validkeys$, keychoice$) = 0 OR keychoice$ = "" ' Process keystroke. SELECT CASE keychoice$ CASE "D" IF NOT inTransaction THEN inTransaction = -1 BEGINTRANS END IF NumSavePts = NumSavePts + 1 SavePts(NumSavePts) = SAVEPOINT DELETE TableNum LOCATE vPos, 7. PRINT msgtxt; NumSavePts; SPC(79 - POS(0)); CASE "Q" EXIT DO CASE "N" MOVENEXT TableNum END SELECT vPos = vPos + 1 LOOP UNTIL EOF(TableNum) OR vPos = viewbottom ' If user didn't delete any records, simply quit. IF NumSavePts = 0 THEN EXIT DO ' Allow user to commit deletions, or roll back some or all of them. VIEW PRINT. LOCATE 24, 1. PRINT "Choose a key. "; PRINT "R - Rollback to a savepoint A - Rollback all deletions" PRINT SPC(17); "Q - commit deletions and Quit"; validkeys$ = "RAQ" DO keychoice$ = UCASE$(INKEY$) LOOP WHILE INSTR(validkeys$, keychoice$) = 0 OR keychoice$ = "" SELECT CASE keychoice$ CASE "R" VIEW PRINT 24 TO 25. PRINT . PRINT DO PRINT "Roll back to which savepoint ( 1 -"; NumSavePts; ")"; INPUT RollbackPt LOOP UNTIL RollbackPt > 0 AND RollbackPt <= NumSavePts ROLLBACK SavePts(RollbackPt) NumSavePts = RollbackPt - 1 CASE "A" NumSavePts = 0 ROLLBACK ALL CASE "Q" EXIT DO END SELECT LOOP IF inTransaction THEN COMMITTRANS CLOSE END